package exercise;

import extend.animal.Cat;

import java.util.Scanner;

/*
定义一个银行帐户类BankAccount实现银行帐户的概念，
在BankAccount类中定义两个变量："帐号" (account) 和密码password 和"存款余额"(money)，
再定义四个方法："存款" (addMoney)、"取款"(minusMoney) 、 "查询余额" (getMoney)、构造方法(BankAccount)。
通过主方法测试:
先登录 根据账号密码判断
失败可以重新登录  三次失败程序结束
登录 成功 显示二级菜单   1.存款   2.取款   3.查询余额 要求 存款取款必须是100的倍数

a.  登录功能
******XXXX银行系统******
1.登录
2.退出
请输入编号:
***************************

b.二级菜单
******XXX登录成功********
1.存款
2.取款
3.查询余额
****************************

 */
public class ex3 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        loop:
        while (true) {
            printLogin();
            BankAccount bankAccount = new BankAccount("123456", "123456", 1000);

            int num = scanner.nextInt();
            switch (num) {
                case 1:
                    login(bankAccount);
                    break;
                case 2:
                    break loop;
                default:
                    System.out.println("输入错误,请重新输入");
            }
        }
    }

    private static void printLogin() {
        System.out.println("******XXXX银行系统******");
        System.out.println("1.登录");
        System.out.println("2.退出");
        System.out.println("请输入编号:");
        System.out.println("***************************");
    }

    private static void login(BankAccount bankAccount) {
        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入账号：");
            String account = scanner.next();
            System.out.println("请输入密码：");
            String password = scanner.next();
            //比对密码
            if (bankAccount.getAccount().equals(account) &&
                    bankAccount.getPassword().equals(password)) {
                loginSuccess(bankAccount);
                return;//登陆成功
            } else {
                System.out.println("账号或密码错误,你还有" + (2 - i) + "次机会");
            }
        }
        //三次失败程序结束
        System.exit(0);
    }

    private static void loginSuccess(BankAccount bankAccount) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            printMenu();
            switch (scanner.nextInt()) {
                case 1:
                    System.out.println("请输入存款金额：");
                    int money = scanner.nextInt();
                    if (money % 100 == 0) {
                        bankAccount.addMoney(money);
                    } else {
                        System.out.println("存款金额必须为100的倍数");
                    }
                    break;
                case 2:
                    System.out.println("请输入取款金额：");
                    int money1 = scanner.nextInt();
                    if (money1 % 100 == 0) {
                        bankAccount.minusMoney(money1);
                    } else {
                        System.out.println("取款金额必须为100的倍数");
                    }
                    break;
                case 3:
                    System.out.println("余额为：" + bankAccount.getMoney());
                    break;
                case 4:
                    return;
                default:
                    System.out.println("输入错误。请重新输入");
            }
        }
    }

    private static void printMenu() {
        System.out.println("******XXX登录成功********");
        System.out.println("1.存款");
        System.out.println("2.取款");
        System.out.println("3.查询余额");
        System.out.println("4.退出");
        System.out.println("****************************");
    }
}

class BankAccount {
    private String account;
    private String password;
    private int money;

    public BankAccount() {
    }

    public BankAccount(String account, String password, int money) {
        this.account = account;
        this.password = password;
        this.money = money;
    }

    public void addMoney(int money) {
        this.money += money;
        System.out.println("存款成功,当前余额：" + this.money);
    }

    public void minusMoney(int money) {
        if (this.money >= money) {
            this.money -= money;
            System.out.println("取款成功,当前余额：" + this.money);
        } else {
            System.out.println("余额不足");
        }
    }

    public int getMoney() {
        return money;
    }

    /**
     * 获取
     *
     * @return account
     */
    public String getAccount() {
        return account;
    }

    /**
     * 设置
     *
     * @param account
     */
    public void setAccount(String account) {
        this.account = account;
    }

    /**
     * 获取
     *
     * @return password
     */
    public String getPassword() {
        return password;
    }

    /**
     * 设置
     *
     * @param password
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * 设置
     *
     * @param money
     */
    public void setMoney(int money) {
        this.money = money;
    }

    public String toString() {
        return "BankAccount{account = " + account + ", password = " + password + ", money = " + money + "}";
    }
}